using TrackSystem.Entities.Extensions;
using TrackSystem.Entities.Models;
using TrackSystem.IO.Formula;

namespace TrackSystem.IO.Alarm;

/// <summary>
/// 报警条目类
/// </summary>
public class AlarmItem
{
    /// <summary>
    /// 统一代码
    /// </summary>
    public Guid Identify { get; set; }
    /// <summary>
    /// 第一次发生该类型报警的时间
    /// </summary>
    public DateTime CreateDt { get; set; }
    /// <summary>
    /// 最近一次发生该报警的时间
    /// </summary>
    public DateTime HappenDt { get; set; }
    /// <summary>
    /// 报警信息
    /// </summary>
    public string? Description { get; set; }
    /// <summary>
    /// 报警类型
    /// </summary>
    public AlarmCategory SourceType { get; set; }
    /// <summary>
    /// 报警来源
    /// </summary>
    public string? SourceName { get; set; }
    /// <summary>
    /// 报警值
    /// </summary>
    public OperandDef? Val { get; set; }
    /// <summary>
    /// 同类报警发生的次数
    /// </summary>
    public int Counter { get; set; }
    /// <summary>
    /// 当前报警是否确认
    /// </summary>
    public bool Confirm { get; set; }
    /// <summary>
    /// 确认该报警的时间
    /// </summary>
    public DateTime ConfirmDt { get; set; }
    /// <summary>
    /// 是否需要隐藏该报警信息
    /// </summary>
    public bool Hidden { get; set; }
    /// <summary>
    /// 隐藏在这个范围内不重新启动报警
    /// </summary>
    public double Range { get; set; }

    /// <summary>
    /// 构造函数
    /// </summary>
    public AlarmItem()
    {
        Identify = Guid.NewGuid();
        CreateDt = DateTime.Now;
        HappenDt = DateTime.Now;
        Counter = 1;
        Confirm = false;
        Hidden = false;
        Range = 0;
        Val = new OperandDef();
    }

    /// <summary>
    /// 格式化输出
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return $"{HappenDt:s}  {Description} 报警来源: {SourceName}, 类型: {SourceType} 【{Counter}]";
    }
    
    /// <summary>
    /// 将数据库中的报警记录复制给报警项目
    /// </summary>
    /// <param name="alarm"></param>
    /// <returns></returns>
    public static AlarmItem CreateAlarmItem(AlarmTable alarm)
    {
        AlarmItem item = new()
        {
            Identify = alarm.Identify,
            CreateDt = alarm.CreateDt,
            HappenDt = alarm.HappenDt,
            Description = alarm.Description,
            SourceName = alarm.SourceName,
            SourceType = alarm.SourceType,
            Counter = alarm.Counter,
            ConfirmDt = alarm.ConfirmDt,
            Confirm = alarm.Confirm,
            Hidden = alarm.Hidden,
            Range = alarm.Range,
        };
        if (OperandDef.TryParse(alarm.Value, out var def))
        {
            item.Val = def;
        }

        return item;
    }


    /// <summary>
    /// 创建数据表
    /// </summary>
    /// <returns></returns>
    public AlarmTable CreateAlarmTable()
    {
        AlarmTable entity = new()
        {
            Identify = Identify,
            CreateDt = CreateDt,
            HappenDt = HappenDt,
            Description = Description,
            SourceType = SourceType,
            SourceName = SourceName,
            Value = (Val is null) ? "" : Val.ToString(),
            Counter = Counter,
            Confirm = Confirm,
            ConfirmDt = ConfirmDt,
            Hidden = Hidden,
            Range = Range
        };   
        return entity;
    }
}